/*
2021-11-12
https://leetcode-cn.com/problems/minimum-operations-to-convert-number/
*/
class Solution {
private:
    int mp[1005];
public:
    int minimumOperations(vector<int>& nums, int start, int goal) {
        queue<int>q;
        q.push(start);
        while(q.size())
        {
            int t=q.front();
            q.pop();
            for(auto temp:nums)
            {
                int a=t+temp;
                if(a==goal) return mp[t]+1;
                if(a>=0 && a<=1000 && !mp[a] && a!=start) mp[a]=mp[t]+1,q.push(a);
                
                int b=t-temp;
                if(b==goal) return mp[t]+1;
                if(b>=0 && b<=1000 && !mp[b] && b!=start) mp[b]=mp[t]+1,q.push(b);
                
                int c=t^temp;
                if(c==goal) return mp[t]+1;
                if(c>=0 && c<=1000 && !mp[c] && c!=start) mp[c]=mp[t]+1,q.push(c);
            }
        }
        
        return -1;
    }
};
